﻿using FashionWebsite.Models;
using FashionWebsite.Models.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FashionWebsite.Controllers
{
    public class ProductManagerController : Controller
    {
        private DalCategory ProductCatelogy = new DalCategory();
        private DalColor Color = new DalColor();
        private DalSize Size = new DalSize();
        private DalProducer producer = new DalProducer();
        private DalProduct listproducts = new DalProduct();
        private DalPage listpages = new DalPage();
        //
        // GET: /ProductManager/
        ShopOnlineDataContext data = new ShopOnlineDataContext();

        public ActionResult Index(int? id, int page = 1, int numPerPage = 5)
        {
            //var products = from c in data.Products
            //                 select c;
            //var viewModel = new DalProduct
            //{
            //    product = products.ToList(),
            //};
            var listpage = listpages.getListPage();
            ViewBag.listPage = listpage;
            var obj = Request.Form["selectPage"];
            IEnumerable<Product> products = DalProduct.GetProducts(id, SearchType.PostedDate, SearchOrder.DESC, page, numPerPage);
            int count = DalProduct.GetProductsCount(id);
            ViewBag.Count = count;
            ViewBag.numberRecordOfPage = numPerPage;
            var viewModel = new DalProduct
            {
                product = products.ToList()
            };
            return View(viewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(int? id)
        {
            var numPerPage = Request.Form["selectPage"];
            var listpage = listpages.getListPage();
            ViewBag.listPage = listpage;
            //var products = from c in data.Products
            //                 select c;
            //var viewModel = new DalProduct
            //{
            //    product = products.ToList(),
            //};
            IEnumerable<Product> products = DalProduct.GetProducts(id, SearchType.PostedDate, SearchOrder.DESC, 1, int.Parse(numPerPage));
            int count = DalProduct.GetProductsCount(id);
            ViewBag.Count = count;
            ViewBag.numberRecordOfPage = int.Parse(numPerPage);
            var viewModel = new DalProduct
            {
                product = products.ToList()
            };
            return View(viewModel);
        }
        //
        // GET: /Product/Create
        //[Authorize]
        public ActionResult Create()
        {
            ViewBag.getstring = ProductCatelogy.getListparent();
            var listsize = Size.getListPSize();
            ViewBag.listSize = listsize;
            var listcategory = ProductCatelogy.getListProductCategory();
            ViewBag.listProductCatelogy = listcategory;
            var listcolor = Color.getListColor();
            ViewBag.listColor = listcolor;
            var listproducer = producer.getListProducer();
            ViewBag.listProducer = listproducer;
            //ViewData["listProductCatelogy"] = list;
            return View();

        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "ProductId")] Product product)
        {
            //map data back to dictionary
            try
            {
                ViewBag.getstring = ProductCatelogy.getListparent() ;
                var listsize = Size.getListPSize();
                ViewBag.listSize = listsize;
                var listcategory = ProductCatelogy.getListProductCategory();
                ViewBag.listProductCatelogy = listcategory;
                var listcolor = Color.getListColor();
                ViewBag.listColor = listcolor;
                var listproducer = producer.getListProducer();
                ViewBag.listProducer = listproducer;
                data.Products.InsertOnSubmit(product);
                data.SubmitChanges();
                return View();
            }
            catch (Exception)
            {
                return View();
            }

        }

        public ActionResult Edit(int id)
        {
            var product = data.Products.SingleOrDefault(x => x.ProductId == id);

            return View(product);


        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, Product product)
        {
            var listproduct = listproducts.getListProducer();
            ViewBag.listproduct = listproduct;
            var products = data.Products.SingleOrDefault(x => x.ProductId.ToString() == id);

            UpdateModel(products);

            data.SubmitChanges();
            return View();
        }

        public ActionResult Delete(string id)
        {
            var product = data.Products.SingleOrDefault(x => x.ProductId.ToString() == id);

            data.Products.DeleteOnSubmit(product);

            data.SubmitChanges();
            return RedirectToAction("Index");
        }
        public ActionResult brower(int id)
        {
            var lproducts = from c in data.Products
                            where c.CategoryId == id
                            select c;
            var viewModel = new DalProduct
            {
                product = lproducts.ToList()
            };
            return View(viewModel);
        
        }
    }
}
